<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Default.php 130 2011-03-18 03:10:02Z cheng.wei $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Storage for authentication
 * 
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Default.php 130 2011-03-18 03:10:02Z cheng.wei $$
 */
class Evebit_Auth_Storage_ClientLogin extends Zend_Auth_Storage_Session {

	/**
	 * @var object
	 */
	protected $_userInstance = null;

	/**
	 * @var array
	 */
	protected $_options = null;

	/**
	 * @var string
	 */
	protected $_modelTypeClass = 'Varien_Object';

	/**
	 * Sets session storage options and initializes session namespace object
	 *
	 * @param  mixed $namespace
	 * @param  mixed $member
	 * @return void
	 */
	public function __construct($options, $member = self::MEMBER_DEFAULT) {
		$this->_options = $options;
		$this->_member = $member;
		$this->_session = Evebit_Application::getSession();
		$this->_namespace = $this->_session->getNamespace();
	}

	/**
	 * @param string $type
	 */
	public function setModelTypeClass($type) {
		$this->_modelTypeClass = $type;
		return $this;
	}

	/**
	 * @return string
	 */
	public function getModelTypeClass() {
		return $this->_modelTypeClass;
	}

	/**
	 * Defined by Zend_Auth_Storage_Interface
	 *
	 * @return mixed
	 */
	public function read() {
		if ($this->_userInstance === null) {
			$type = $this->getModelTypeClass();
			$this->_userInstance = new $type();
			$data = $this->_session->{$this->_member};
			$userId = $data['id'];
			if ($userId) {
				if (is_callable(array($this->_userInstance, 'load'))) {
					$this->_userInstance->load($userId);
				} elseif ($this->_userInstance instanceof Varien_Object) {
					$this->_userInstance->setId($userId);
				} elseif (is_callable(array($this->_userInstance, 'setId'))) {
					$this->_userInstance->setId($userId);
				} else {
					$this->_userInstance->id = $userId;
				}
			}
		}
		return $this->_userInstance;
	}

	/**
	 * Get http client for Zend_Gdata
	 * It could be used to access Google Calendar
	 * 
	 * @return Zend_Gdata_HttpClient
	 */
	public function getHttpClient() {
		
		// Zend_Http_Client options
		$options = array(
				'adapter'=> 'Zend_Http_Client_Adapter_Curl', 
				'maxredirects'=> 3, 
				'timeout'=> 15, 
				'useragent'=> 'china-wanderer', 
				'curloptions'=> array(CURLOPT_SSL_VERIFYHOST=> 1, CURLOPT_SSL_VERIFYPEER=> false));
		
		// Set proxy
		if ($proxy = Evebit_Application::config("httpclient_proxy")) {
			$options['curloptions'][CURLOPT_PROXY] = $proxy;
			$options['curloptions'][CURLOPT_PROXYTYPE] = (int) Evebit_Application::config(
				"httpclient_proxytype");
		}
		$client = new Zend_Gdata_HttpClient(null, $options);
		if (!$this->isEmpty()) {
			$data = $this->_session->{$this->_member};
			$client->setClientLoginToken($data['token']);
		}
		
		return $client;
	}
	
	/**
	 * Get user id
	 * 
	 * @return int
	 */
	public function getUserId() {
		if (!$this->isEmpty()) {
			$data = $this->_session->{$this->_member};
			return $data['id'];
		} 
	}
	
	/**
	 * Get user gmail address
	 * 
	 * @return string
	 */
	public function getUserEmail() {
		if (!$this->isEmpty()) {
			$data = $this->_session->{$this->_member};
			return $data['email'];
		}
	}
	
	/**
	 * Get user gmail password
	 * 
	 * @return string
	 */
	public function getUserPassword() {
		if (!$this->isEmpty()) {
			$data = $this->_session->{$this->_member};
			return $data['password'];
		}
	}
}

